﻿
<script>
    var _vm;
    //-- $(function (){ 是$(document).ready(function()的简写，这个是在页面DOM文档加载完成后加载执行的
    $(function () {
        var vm = new ViewModel();
        _vm = vm;
        ko.applyBindings(vm); // 激活Knockout
        vm.Init();
    });       

    //-- JS自定义函数
    function ViewModel() {
        var me = this;                      // 该写法是为了提高压缩率

        // ace.js里的function
        ViewModelBase.call(me);             // call()用于调用当前函数 详细内容查阅 http://www.365mini.com/page/javascript-function-call.htm
                                            // https://segmentfault.com/q/1010000005721719
        vmExtend.call(me);

        me.DeleteUrl = "@this.ContentUrl("~/BikeClub/Activity/Delete")";
        me.DataTable = new DataTableBase(me);
        me.Dialog = new Dialog(me);
    }

    function vmExtend() {
        var me = this;

        me.Init = function () {
            me.LoadModels();                // 调用 本脚本 自定义函数 LoadModels()
        }

        me.LoadModels = function (page) {   // 按页 查询所有内容,也按照关键字查询
            me.DataTable.SelectedModel(null);
            var data = me.SearchModel();
            //data.page = page || 1;
            $ace.get("@this.ContentUrl("~/BikeClub/Activity/GetModels")", data, function (result) {
                me.DataTable.SetModels(result.Data);        // 调用 $ko.toOb 它调用 ko.mapping.fromJS
            }
          );
        }
    }

    function Dialog(vm) {
        var me = this;
        DialogBase.call(me);

        me.OnOpen = function () {

            var model = me.EditModel();
            if (model) {
                var bindModel = $ko.toJS(model);        //把map过的对象转换成原来的JavaScript对象
                me.Model(bindModel);
            }
            else {
                me.Model();
            }

            //初始化
            initControl();

        }

        me.OnSave = function () {
            var model = me.Model();

            if (!$('#form1').formValid()) {
                return false;
            }

            model.People = $("#People").val();

            if (me.EditModel()) {
                $ace.post("@this.ContentUrl("~/BikeClub/Activity/Update")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
               );
            }
            else {
                $ace.post("@this.ContentUrl("~/BikeClub/Activity/Add")", model, function (result) {
                    $ace.msg(result.Msg);
                    me.Close();
                    vm.LoadModels();
                }
             );
            }
        }

        function initControl() {

            $("#People2").val(null);
            var People = $("#People").val();     // 取得人员的值
            // 初始化时设置默认值
            var data = [];
            if (People != "") {
                data = People.split(",");
            }
            //初始化选中的人
            $("#People2").val(data).trigger("change");

            $("#People2").select2({
                placeholder: '请选择',
                multiple: true,
                //separator: ","
                //tags: true
                //allowClear: true,
            });

            $("#People2").change(function () {
                // 先取得People2的值
                // 转成 id,id,id,id
                // 数组转成字符串,用逗号连接, People = data.join(",");
                if ($("#People2").val() != null) {
                    People = $("#People2").val().join(",");
                    $("#People").val(People);
                }
                else $("#People").val(null);
            });
        }
    }

</script>

